package org.itboys.admin.service.lasted;

import org.itboys.admin.entity.lasted.*;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年2月27日
 * 联系方式 ：empty90911@126.com
 * 描述：系统_index(首页)_Service_，江雄重新写的
 */
@Service
public class SystemIndexService {
      @Autowired
      private SystemUserService userService;
      @Autowired
      private SystemMenuService menuService;
      @Autowired
      private SystemUserRoleService userRoleService;
      @Autowired
      private SystemRolePermissionService rolePermissionService;

    /**
     * 获取当前用户可用菜单
     * @param userId
     * @return
     */
    public List<SystemMenu> getUserAllowMenu(Long userId){
        List<SystemMenu> allowMenus = new ArrayList<SystemMenu>();


          if(LongUtil.isNotZreo(userId)){
                //admin或超级用户不校验权限
              SystemUser user=userService.getById(userId);
              if(user.getIsSuper()!=1){
                  Map<String,Object> param = new HashMap<String,Object>();
                  //获取用户的角色
                  param.put("userId",userId);
                  List<SystemUserRole> userRoles= userRoleService.list(param);
                  List<Long> roleIds = new ArrayList<>();
                  for(SystemUserRole userRole:userRoles){
                      roleIds.add(userRole.getRoleId());
                  }
                  if(ListUtil.isNotNull(roleIds)){
                      param.clear();
                      param.put("roleId in",roleIds);
                      param.put("type",SystemRolePermission.system_role_permission_type_menu);
                      List<SystemRolePermission> rolePermissions=rolePermissionService.list(param);
                      List<Long> menuIds = new ArrayList<Long>();
                      for(SystemRolePermission rolePermission:rolePermissions){
                          menuIds.add(rolePermission.getMenuId());

                      }
                      allowMenus= menuService.getTree(menuIds);
                  }
              }else{
                      allowMenus=menuService.getTree(null);
              }

          }
          return allowMenus;
      }

}
